﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
using Zhaoxi.NLogSample.IOCLog;
using Zhaoxi.NLogSample.NoIOCLog;
using LogLevel = Microsoft.Extensions.Logging.LogLevel;

#region IOC的日志输出
{
    var servicesProvider = new ServiceCollection() // 服务集合
    .AddLogging(builder => // 注册日志服务
    {
        builder.AddNLog(); // 将配置文件注册到日志服务中
    })
    .AddScoped<TestLog>()
    .BuildServiceProvider();

    var logger = servicesProvider.GetRequiredService<ILogger<Program>>(); // 获取日志对象

    var leveles = (LogLevel[])Enum.GetValues(typeof(LogLevel)); // 获取日志级别
    var eventId = 1;
    foreach (var level in leveles) // 遍历日志级别
    {
        logger.Log(level, eventId++, $"这是一条{level}消息");
        //logger.Log(level, $"这是一条{level}消息");
    }

    var testLog = servicesProvider.GetRequiredService<TestLog>();
    testLog.Show();
}
#endregion

#region 非IOC的日志输出
{
    TestLog2 testLog2 = new TestLog2();
    testLog2.Show();
}
#endregion

